package com.example.wisdomclassroomapi.domain.repository;

import com.example.wisdomclassroomapi.domain.entity.CourseMaterialEntity;
import com.example.wisdomclassroomapi.infrastructure.mapper.CourseMaterialMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.List;
/**
 * @Package: com.example.wisdomclassroomapi.domain.repository
 * @Description: <>
 * @Author: scholes
 * @CreateDate: 2022-04-18
 * @UpdateUser: scholes
 * @UpdateDate: 2022-04-18
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@Repository
public class CourseMaterialRepository {
    @Resource
    private CourseMaterialMapper courseMaterialMapper;

    public void add(CourseMaterialEntity entity) {
       courseMaterialMapper.insert(entity);
    }

    public CourseMaterialEntity findById(String id) {
        return courseMaterialMapper.selectById(id);
    }

    public int updateById(CourseMaterialEntity entity) {
        return courseMaterialMapper.updateById(entity);
    }

    public int deleteById(String id) {
        return courseMaterialMapper.deleteById(id);
    }

    public IPage<CourseMaterialEntity> page(CourseMaterialEntity entity,IPage page) {
        QueryWrapper<CourseMaterialEntity> wrapper = new QueryWrapper<>();
            if (StringUtils.isNotBlank(entity.getLessonId())){
            wrapper.eq("lesson_id",entity.getLessonId());
            }
            if (StringUtils.isNotBlank(entity.getType())){
            wrapper.eq("type",entity.getType());
            }
            if (StringUtils.isNotBlank(entity.getUrl())){
            wrapper.eq("url",entity.getUrl());
            }
            if (StringUtils.isNotBlank(entity.getDesc())){
            wrapper.eq("desc",entity.getDesc());
            }
        return courseMaterialMapper.selectPage(page, wrapper);
    }

    public List<CourseMaterialEntity> list(CourseMaterialEntity entity) {
        QueryWrapper<CourseMaterialEntity> wrapper = new QueryWrapper<>();
            if (StringUtils.isNotBlank(entity.getLessonId())){
            wrapper.eq("lesson_id",entity.getLessonId());
            }
            if (StringUtils.isNotBlank(entity.getType())){
            wrapper.eq("type",entity.getType());
            }
            if (StringUtils.isNotBlank(entity.getUrl())){
            wrapper.eq("url",entity.getUrl());
            }
            if (StringUtils.isNotBlank(entity.getDesc())){
            wrapper.eq("desc",entity.getDesc());
            }
        return courseMaterialMapper.selectList(wrapper);
    }

}